﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Giaiphongmatbang.BLL;
using Giaiphongmatbang.DataModel;

namespace Giaiphongmatbang
{
    public partial class Thaydoimatkhau : System.Web.UI.Page
    {
        QLGPMBEntities db = new QLGPMBEntities();
        User user;
        protected void Page_Load(object sender, EventArgs e)
        {
            user = (User)Session["User"];
            if (!IsPostBack)
            {
                if (user != null)
                {
                    ltrUserName.Text = user.UserName;
                    txtoldPass.Focus();
                }
            }
        }

        protected void btnApply_Click(object sender, EventArgs e)
        {
            if (user.Password == Utility.Encryptions.EncryptUTF8MD5(txtoldPass.Text))
            {
                User newU = db.Users.FirstOrDefault(p => p.UserID == user.UserID);
                newU.Password = Utility.Encryptions.EncryptUTF8MD5(txtnewPass.Text);
                db.SaveChanges();
                Session["User"] = newU;
                ltrStatus.Text = "";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('Đổi mật khẩu thành công')", true);
            }
            else
            {
                ltrStatus.Text = "<label style='color:Red;'>Mật khẩu chưa đúng</label>";
            }
        }
    }
}